
*This file conducts the baseline estimation

*Bring in regression data
use regression_data.dta, clear

*drop ROW as an originId
drop if originId==62

egen clusterId=group(originId)

************************************************************************************

*Collapsed version: average across importers
collapse logX_theta logL* instrument_* logX_adj logZ_adj clusterId theta se_gyy, by(year sectorId originId) 
mkmat se_gyy if originId==1 & year==2010 & sectorId>2, matrix(S)
mkmat theta if originId==1 & year==2010 & sectorId>2, matrix(T)

**************************************************************************************
*Multiple Elasticities: Heterogeneous TE
*Tables 1
**************************************************************************************

*FE
local dvars logX_theta
local indvars c.logL#sectorId

*OLS
reghdfe `dvars' `indvars' if sectorId>2 & year==2010, cluster(clusterId) nocons ///
a(sectorId#year originId#year)
matrix list e(b)


*Save statistics for table
matrix bo=e(b)
matrix AO=e(V)

*CES Instrument
local instruments c.instrument_base#sectorId
ivreghdfe `dvars'  (`indvars'=`instruments') if sectorId>2 & year==2010, cluster(clusterId) absorb(sectorId#year originId#year) sfirst

*Save statistics for table
matrix A=e(V)
matrix b=e(b)
matrix f=e(first)
matrix list f
matrix F=f["F",1...]'
matrix SWF=f["SWF",1...]'


*Reduced form
reghdfe `dvars'  `instruments' if sectorId>2 & year==2010, cluster(clusterId) ///
a(sectorId#year originId#year) nocons


*Save statistics for table
matrix AR=e(V)
matrix br=e(b)


*************************************************************************************
*****Need to compute standard errors corrected for the standard errors of the TEs
*************************************************************************************

preserve
keep if year==2010 & sectorId>2
keep originId sectorId theta logX_theta logL instrument_base
sort sectorId

*First step: make matrix of instruments and independent variables, plus the matrix of the derivative of the dependent
*variable with respect to the trade elasticity vector

forvalues x=3/17	{
	gen logX_theta_`x'=-1/theta*logX_theta if sectorId==`x'
	replace logX_theta_`x'=0 if logX_theta_`x'==.
	gen logL_`x'=logL if sectorId==`x'
	replace logL_`x'=0 if logL_`x'==.
	gen instrument_base_`x'=instrument_base if sectorId==`x'
	replace instrument_base_`x'=0 if instrument_base_`x'==.	
}

*Second step: residualize the logX_theta`x' and logL_`x' with respect to the fixed effects

forvalues x=3/17	{	
	reghdfe logX_theta_`x' if sectorId>2,  ///
	absorb(sectorId originId) res(logX_thetahat_`x') 
	reghdfe logL_`x' if sectorId>2,  ///
	absorb(sectorId originId) res(logLhat_`x') 
	
}

reghdfe logX_theta if sectorId>2,  ///
absorb(sectorId originId) res(y) 


*Third step: construct matrices of instruments and residualized independent and dependent variables
mkmat instrument_base_*, matrix(Z)
mkmat logX_thetahat_*, matrix (Y)
mkmat logLhat_*, matrix(L)
mkmat y, matrix(y)

*Fourth Step: Compute the Jacobian matrices for OLS and IV and the reduced form
matrix J = -inv(Z'*L)*Z'*Y
matrix JO = -inv(L'*L)*L'*Y
matrix JR = -inv(Z'*Z)*Z'*Y

*Compute the variance-covariance matrix and the standard errors

matrix VD = vecdiag(A+J*diag(S)*diag(S)*J')
mata : st_matrix("SE",  st_matrix("VD"):^(.5))
matrix VDO = vecdiag(AO+JO*diag(S)*diag(S)*JO')
mata : st_matrix("SEO",  st_matrix("VDO"):^(.5))
matrix VDR = vecdiag(AR+JR*diag(S)*diag(S)*JR')
mata : st_matrix("SER",  st_matrix("VDR"):^(.5))

*Preparing for export

matrix M=T'\S'
mata: st_matrix("m", colshape(st_matrix("M")', 30))
matrix M = bo\SEO
mata: st_matrix("m1", colshape(st_matrix("M")', 30))
matrix M = b\SE
mata: st_matrix("m2", colshape(st_matrix("M")', 30))
matrix M = br\SER
mata: st_matrix("m3", colshape(st_matrix("M")', 30))

matrix M=(m\m1\m2\m3)'
matrix list M

putexcel set table3, sheet(SE Estimates) replace
putexcel A1=matrix(M)
putexcel F1=matrix(F)
putexcel G1=matrix(SWF)
restore



******************************************************************************************************************************************
*Now doing individual years with IV
*Table B.4
*****************************************************************************************************************************************

****************
*1995
*****************
*FE
local dvars logX_theta
local indvars c.logL#sectorId
local instruments c.instrument_base#sectorId

ivreghdfe `dvars'  (`indvars'=`instruments') if sectorId>2 & year==1995, cluster(clusterId) absorb(sectorId#year originId#year) sfirst

*Save statistics for table
matrix A=e(V)
matrix b=e(b)

*************************************************************************************
*****Need to compute standard errors corrected for the standard errors of the TEs
*************************************************************************************

preserve
keep if year==1995 & sectorId>2
keep originId sectorId theta logX_theta logL instrument_base
sort sectorId

*First step: make matrix of instruments and independent variables, plus the matrix of the derivative of the dependent
*variable with respect to the trade elasticity vector

forvalues x=3/17	{
	gen logX_theta_`x'=-1/theta*logX_theta if sectorId==`x'
	replace logX_theta_`x'=0 if logX_theta_`x'==.
	gen logL_`x'=logL if sectorId==`x'
	replace logL_`x'=0 if logL_`x'==.
	gen instrument_base_`x'=instrument_base if sectorId==`x'
	replace instrument_base_`x'=0 if instrument_base_`x'==.	
}

*Second step: residualize the logX_theta`x' and logL_`x' with respect to the fixed effects

forvalues x=3/17	{	
	reghdfe logX_theta_`x' if sectorId>2,  ///
	absorb(sectorId originId) res(logX_thetahat_`x') 
	reghdfe logL_`x' if sectorId>2,  ///
	absorb(sectorId originId) res(logLhat_`x') 
	reghdfe instrument_base_`x' if sectorId>2,  ///
	absorb(sectorId originId) res(instrument_basehat_`x') 
	
}

reghdfe logX_theta if sectorId>2,  ///
absorb(sectorId originId) res(y) 

*Third step: construct matrices of instruments and residualized independent and dependent variables
mkmat instrument_base_*, matrix(Z)
mkmat instrument_basehat_*, matrix(ZF)
mkmat logX_thetahat_*, matrix (Y)
mkmat logLhat_*, matrix(L)
mkmat y, matrix(y)

*Fourth Step: Compute the Jacobian matrices for IV
matrix J = -inv(Z'*L)*Z'*Y

*Compute the variance-covariance matrix and the standard errors

matrix VD = vecdiag(A+J*diag(S)*diag(S)*J')
mata : st_matrix("SE",  st_matrix("VD"):^(.5))

matrix M = b\SE
mata: st_matrix("m2", colshape(st_matrix("M")', 30))
matrix M=m2'


putexcel set table3_years, sheet(SE Estimates) replace
putexcel A1=matrix(M)
restore

**********************************
*2000
*********************************

*FE
local dvars logX_theta
local indvars c.logL#sectorId
local instruments c.instrument_base#sectorId

ivreghdfe `dvars'  (`indvars'=`instruments') if sectorId>2 & year==2000, cluster(clusterId) absorb(sectorId#year originId#year) sfirst

*Save statistics for table
matrix A=e(V)
matrix b=e(b)

*************************************************************************************
*****Need to compute standard errors corrected for the standard errors of the TEs
*************************************************************************************

preserve
keep if year==2000 & sectorId>2
keep originId sectorId theta logX_theta logL instrument_base
sort sectorId

*First step: make matrix of instruments and independent variables, plus the matrix of the derivative of the dependent
*variable with respect to the trade elasticity vector

forvalues x=3/17	{
	gen logX_theta_`x'=-1/theta*logX_theta if sectorId==`x'
	replace logX_theta_`x'=0 if logX_theta_`x'==.
	gen logL_`x'=logL if sectorId==`x'
	replace logL_`x'=0 if logL_`x'==.
	gen instrument_base_`x'=instrument_base if sectorId==`x'
	replace instrument_base_`x'=0 if instrument_base_`x'==.	
}

*Second step: residualize the logX_theta`x' and logL_`x' with respect to the fixed effects

forvalues x=3/17	{	
	reghdfe logX_theta_`x' if sectorId>2,  ///
	absorb(sectorId originId) res(logX_thetahat_`x') 
	reghdfe logL_`x' if sectorId>2,  ///
	absorb(sectorId originId) res(logLhat_`x') 
	reghdfe instrument_base_`x' if sectorId>2,  ///
	absorb(sectorId originId) res(instrument_basehat_`x') 
	
}

reghdfe logX_theta if sectorId>2,  ///
absorb(sectorId originId) res(y) 

*Third step: construct matrices of instruments and residualized independent and dependent variables
mkmat instrument_base_*, matrix(Z)
mkmat instrument_basehat_*, matrix(ZF)
mkmat logX_thetahat_*, matrix (Y)
mkmat logLhat_*, matrix(L)
mkmat y, matrix(y)

*Fourth Step: Compute the Jacobian matrices for IV
matrix J = -inv(Z'*L)*Z'*Y

*Compute the variance-covariance matrix and the standard errors

matrix VD = vecdiag(A+J*diag(S)*diag(S)*J')
mata : st_matrix("SE",  st_matrix("VD"):^(.5))

matrix M = b\SE
mata: st_matrix("m2", colshape(st_matrix("M")', 30))
matrix M=m2'

putexcel B1=matrix(M)
restore

**********************************
*2005
*********************************

*FE
local dvars logX_theta
local indvars c.logL#sectorId
local instruments c.instrument_base#sectorId

ivreghdfe `dvars'  (`indvars'=`instruments') if sectorId>2 & year==2005, cluster(clusterId) absorb(sectorId#year originId#year) sfirst
*outreg2  using ${tab_dir}/table3.xml, append excel nor2 dec(2)  addstat(F-stat, e(widstat)) noaster nopa

*Save statistics for table
matrix A=e(V)
matrix b=e(b)

*************************************************************************************
*****Need to compute standard errors corrected for the standard errors of the TEs
*************************************************************************************

preserve
keep if year==2005 & sectorId>2
keep originId sectorId theta logX_theta logL instrument_base
sort sectorId

*First step: make matrix of instruments and independent variables, plus the matrix of the derivative of the dependent
*variable with respect to the trade elasticity vector

forvalues x=3/17	{
	gen logX_theta_`x'=-1/theta*logX_theta if sectorId==`x'
	replace logX_theta_`x'=0 if logX_theta_`x'==.
	gen logL_`x'=logL if sectorId==`x'
	replace logL_`x'=0 if logL_`x'==.
	gen instrument_base_`x'=instrument_base if sectorId==`x'
	replace instrument_base_`x'=0 if instrument_base_`x'==.	
}

*Second step: residualize the logX_theta`x' and logL_`x' with respect to the fixed effects

forvalues x=3/17	{	
	reghdfe logX_theta_`x' if sectorId>2,  ///
	absorb(sectorId originId) res(logX_thetahat_`x') 
	reghdfe logL_`x' if sectorId>2,  ///
	absorb(sectorId originId) res(logLhat_`x') 
	reghdfe instrument_base_`x' if sectorId>2,  ///
	absorb(sectorId originId) res(instrument_basehat_`x') 
	
}

reghdfe logX_theta if sectorId>2,  ///
absorb(sectorId originId) res(y) 

*Third step: construct matrices of instruments and residualized independent and dependent variables
mkmat instrument_base_*, matrix(Z)
mkmat instrument_basehat_*, matrix(ZF)
mkmat logX_thetahat_*, matrix (Y)
mkmat logLhat_*, matrix(L)
mkmat y, matrix(y)

*Fourth Step: Compute the Jacobian matrices for IV
matrix J = -inv(Z'*L)*Z'*Y

*Compute the variance-covariance matrix and the standard errors

matrix VD = vecdiag(A+J*diag(S)*diag(S)*J')
mata : st_matrix("SE",  st_matrix("VD"):^(.5))

matrix M = b\SE
mata: st_matrix("m2", colshape(st_matrix("M")', 30))
matrix M=m2'

putexcel C1=matrix(M)
restore

**********************************
*ALL YEARS
*********************************

*FE
local dvars logX_theta
local indvars c.logL#sectorId
local instruments c.instrument_base#sectorId

ivreghdfe `dvars'  (`indvars'=`instruments') if sectorId>2, cluster(clusterId) absorb(sectorId#year originId#year) sfirst

*Save statistics for table
matrix A=e(V)
matrix b=e(b)

*************************************************************************************
*****Need to compute standard errors corrected for the standard errors of the TEs
*************************************************************************************

preserve
keep if sectorId>2
keep originId sectorId theta logX_theta logL instrument_base
sort sectorId

*First step: make matrix of instruments and independent variables, plus the matrix of the derivative of the dependent
*variable with respect to the trade elasticity vector

forvalues x=3/17	{
	gen logX_theta_`x'=-1/theta*logX_theta if sectorId==`x'
	replace logX_theta_`x'=0 if logX_theta_`x'==.
	gen logL_`x'=logL if sectorId==`x'
	replace logL_`x'=0 if logL_`x'==.
	gen instrument_base_`x'=instrument_base if sectorId==`x'
	replace instrument_base_`x'=0 if instrument_base_`x'==.	
}

*Second step: residualize the logX_theta`x' and logL_`x' with respect to the fixed effects

forvalues x=3/17	{	
	reghdfe logX_theta_`x' if sectorId>2,  ///
	absorb(sectorId originId) res(logX_thetahat_`x') 
	reghdfe logL_`x' if sectorId>2,  ///
	absorb(sectorId originId) res(logLhat_`x') 
	reghdfe instrument_base_`x' if sectorId>2,  ///
	absorb(sectorId originId) res(instrument_basehat_`x') 
	
}

reghdfe logX_theta if sectorId>2,  ///
absorb(sectorId originId) res(y) 

*Third step: construct matrices of instruments and residualized independent and dependent variables
mkmat instrument_base_*, matrix(Z)
mkmat instrument_basehat_*, matrix(ZF)
mkmat logX_thetahat_*, matrix (Y)
mkmat logLhat_*, matrix(L)
mkmat y, matrix(y)

*Fourth Step: Compute the Jacobian matrices for IV
matrix J = -inv(Z'*L)*Z'*Y

*Compute the variance-covariance matrix and the standard errors

matrix VD = vecdiag(A+J*diag(S)*diag(S)*J')
mata : st_matrix("SE",  st_matrix("VD"):^(.5))

matrix M = b\SE
mata: st_matrix("m2", colshape(st_matrix("M")', 30))
matrix M=m2'

putexcel D1=matrix(M)
restore

**********************************************************************************************************************
**********************************************************************************************
*Now with intermediate goods and capital, SE in gross input
*Table B.7
**********************************************************************************************

*FE
local dvars logX_adj
local indvars c.logZ_adj#sectorId

*set matsize 11000

*OLS
reghdfe `dvars' `indvars' if sectorId>2 & year==2010, cluster(clusterId) ///
a(sectorId#year originId#year) nocons

matrix AO=e(V)
matrix bo=e(b)

*CES Instrument
local instruments c.instrument_intermediate#sectorId
ivreghdfe `dvars' (`indvars'=`instruments') if sectorId>2 & year==2010, cluster(clusterId) ///
a(sectorId#year originId#year) sfirst

*Save statistics for table
matrix A=e(V)
matrix b=e(b)
matrix f=e(first)
matrix list f
matrix F=f["F",1...]'
matrix SWF=f["SWF",1...]'

*************************************************************************************
*****Need to compute standard errors corrected for the standard errors of the TEs
*************************************************************************************

preserve
keep if year==2010 & sectorId>2
keep originId sectorId theta logX_adj logZ_adj instrument_intermediate

*First step: make matrix of instruments and independent variables, plus the matrix of the derivative of the dependent
*variable with respect to the trade elasticity vector

forvalues x=3/17	{
	gen logX_adj_`x'=-1/theta*logX_adj if sectorId==`x'
	replace logX_adj_`x'=0 if logX_adj_`x'==.
	gen logZ_adj_`x'=logZ_adj  if sectorId==`x'
	replace logZ_adj_`x'=0 if logZ_adj_`x'==.
	gen instrument_intermediate_`x'=instrument_intermediate if sectorId==`x'
	replace instrument_intermediate_`x'=0 if instrument_intermediate_`x'==.	
}

*Second step: residualize the logX_theta`x' and logL_`x' with respect to the fixed effects

forvalues x=3/17	{	
	reghdfe logX_adj_`x' if sectorId>2,  ///
	absorb(sectorId originId) res(logX_adjhat_`x') 
	reghdfe logZ_adj_`x' if sectorId>2,  ///
	absorb(sectorId originId) res(logZ_adjhat_`x') 
	
}

reghdfe logX_adj if sectorId>2,  ///
absorb(sectorId originId) res(y) 


*Third step: construct matrices of instruments and residualized independent and dependent variables
mkmat instrument_intermediate_*, matrix(Z)
mkmat logX_adjhat_*, matrix (Y)
mkmat logZ_adjhat_*, matrix(L)
mkmat y, matrix(y)

*Fourth Step: Compute the Jacobian matrices for OLS and IV and the reduced form
matrix J = -inv(Z'*L)*Z'*Y
matrix JO = -inv(L'*L)*L'*Y
matrix JR = -inv(Z'*Z)*Z'*Y

*Compute the variance-covariance matrix and the standard errors

matrix VD = vecdiag(A+J*diag(S)*diag(S)*J')
mata : st_matrix("SE",  st_matrix("VD"):^(.5))
matrix VDO = vecdiag(AO+JO*diag(S)*diag(S)*JO')
mata : st_matrix("SEO",  st_matrix("VDO"):^(.5))


*Preparing for export

matrix M = bo\SEO
mata: st_matrix("m1", colshape(st_matrix("M")', 30))
matrix M = b\SE
mata: st_matrix("m2", colshape(st_matrix("M")', 30))

matrix M=(m1\m2)'
matrix list M

putexcel set table4, sheet(SE Estimates) replace
putexcel A1=matrix(M)
putexcel F1=matrix(F)
putexcel G1=matrix(SWF)
restore



